articles

Home / DeveloperSection / Articles / Abstraction in oops c#

Abstraction in oops c#

Abstraction in oops c#

Rashmi Sharma844 14-May-2021

Abstraction :-

It Displays only necessary details and hides unnecessary to the outside world. Abstraction is about to show only essential details and functionality to the user. It provides hiding the implementation from the user. when we are hiding the details use private access modifier.

Abstract Class :-

A class which contains any abstract members in it is known as abstract class.

An abstract class is a partially implemented class used for developing some of the operations of an object which are common for all next level subclasses.

For ex :- Rectangle, Circle, Triangle and cone.

Entities:

Rectangle : Width, Height

Circle : Radius, Pi

Triangle : Width, Height

Cone : Radius, height, Pi

Common Attributes :

Width, Height, radius, Pi

Abstract Method :-

1. A method does not have a body is called an abstract method. A method have abstract access modifier.

2. Overriding an abstract method is compulsory.

For example :- 

 public abstract class Figure

    {

        public double Width, Height, Radius;

        public const float pi= 3.14f;

        public abstract double GetArea();

    }

    public class Rectangle : Figure

    {

        public Rectangle(double width, double height)

        {

            this.Width = width;

            this.Height = height;

        }

        public override double GetArea()

        {

            return Height * Width;

        }

    }

    public class Circle : Figure

    {

        public Circle(double Radius)

        {

            this.Radius = Radius;

        }

        public override double GetArea()

        {

            return Radius*pi*pi;

        }

    }

    public class Triangle : Figure

    {

        public Triangle(double Width,double Height)

        {

            this.Width = Width;

            this.Height = Height;

        }

        public override double GetArea()

        {

            return (Width * Height) / 2;

        }

    }

    public class Cone : Figure

    {

        public Cone(double Radius, double Height)

        {

            this.Radius = Radius;

            this.Height = Height;

        }

        public override double GetArea()

        {

            return pi*Radius * (Radius+Math.Sqrt(Height*Height+Radius*Radius));

        }

    }

class Program 

 {

        static void Main(string[] args)

        {

               Rectangle rc = new Rectangle(5.62, 6.77);

               Circle cr = new Circle(5.62);

               Triangle tr = new Triangle(8.62, 8.77);

               Cone cn = new Cone(2.62, 4.77);


            Console.WriteLine('Area of Rectangle ' + rc.GetArea());

            Console.WriteLine('Area of Circle ' + cr.GetArea());

            Console.WriteLine('Area of Triangle ' + tr.GetArea());

            Console.WriteLine('Area of Cone ' + cn.GetArea());       

    }

 }

Output :-

Abstraction in oops c#



My Hometown is Agra. i have done my schooling from John Milton Public school, Agra. After that my Graduation(B.C.A) specialised in Computer Application was done from Aryan institute of management and computer studies and Post Graduation(M.C.A) specialised in Computer Application from Hindustan Institute of management and Computer Studies. I have started my Career as Programming Trainer in the Computer Training Institute around 2 years from 2015 to 2017 and changed to dot net developer

Leave Comment

Comments

Liked By